
const { app, BrowserWindow, screen } = require('electron');
const path = require('path')

const EnvContext = require('../../../common/context/EnvContext');
const { mainWinConst } = require('../../../common/constans/mainWinConstans')
const { createView } = require('./createWebView')


exports.createMainWin = function () {

    const preloadPath = path.join(__dirname, '../../../preload/preload.js')

    const windowOptions = {
        width: mainWinConst.baseWidth,
        height: mainWinConst.baseHeight,
        minHeight: mainWinConst.baseHeight,
        maxWidth: mainWinConst.baseMaxWidth,
        minWidth: mainWinConst.baseWidth,
        frame: false,
        show: false,
        skipTaskbar: true,
        useContentSize: true,
        autoHideMenuBar: true,
        webPreferences: {
            spellcheck: false,
            webSecurity: false,
            enableRemoteModule: true,
            backgroundThrottling: false,
            contextIsolation: true,
            webviewTag: true,
            nodeIntegration: true,
            nodeintegrationinsubframes: true,
            preload: preloadPath

        }

    };
    let mainWindow = new BrowserWindow(windowOptions);
    const theScrean = screen.getDisplayNearestPoint({ x: screen.getCursorScreenPoint().x, y: screen.getCursorScreenPoint().y })
    EnvContext.getInstance().setLastScreanId(theScrean.id)
    mainWindow.loadURL("http://localhost:8000/");

    mainWindow.getBounds().width


    // mainWindow.loadURL(path.join('file://', __dirname, '/build/index.html'));
    //接收渲染进程的信息
    const ipc = require('electron').ipcMain;
    ipc.on('min', function () {
        mainWindow.minimize();
    });
    ipc.on('max', function () {
        mainWindow.maximize();
    });
    ipc.on("login", function () {
        mainWindow.maximize();
    });


    mainWindow.on('closed', () => {
        mainWindow = null
    })

    // mainWindow.on('blur', () => {
    //     mainWindow.hide()
    // })

    mainWindow.once('ready-to-show', () => {
        mainWindow.show()
        // 非隐藏式启动需要显示主窗口
        if (!app.getLoginItemSettings().wasOpenedAsHidden) {
            mainWindow.show()
        }
    })
    EnvContext.getInstance().setMainWin(mainWindow)

    createView(mainWindow, preloadPath, "http://localhost:8000/list", "main")

}
